package com.mmm.user.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mmm.common.vo.Paging;
import com.mmm.user.dao.UserDao;
import com.mmm.user.entity.Delivery;
import com.mmm.user.dao.DeliveryDao;
import com.mmm.user.entity.User;
import com.mmm.user.service.DeliveryService;
import com.mmm.user.service.JwtService;
import com.mmm.user.service.TokenService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (Delivery)表服务实现类
 *
 * @author One
 * @since 2022-02-27 10:58:04
 */
@Service("deliveryService")
@Transactional
public class DeliveryServiceImpl implements DeliveryService {
    @Resource
    private DeliveryDao deliveryDao;
    @Resource
    private JwtService jwtService;
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Delivery queryById(Long id) {
        return this.deliveryDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Delivery> queryAllByLimit(int offset, int limit) {
        return this.deliveryDao.queryAllByLimit(offset, limit);
    }



    /**
     * 新增数据
     *
     * @param delivery 实例对象
     * @return 实例对象
     */
    @Override
    public Delivery insert(Delivery delivery) {
        Date date = new Date();
        delivery.setCreateTime(date);
        delivery.setUpdateTime(date);
        this.deliveryDao.insert(delivery);
        return delivery;
    }

    /**
     * 修改数据
     *
     * @param delivery 实例对象
     * @return 实例对象
     */
    @Override
    public Delivery update(Delivery delivery) {
        delivery.setUpdateTime(new Date());
        this.deliveryDao.update(delivery);
        return this.queryById(delivery.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        Delivery delivery = this.queryById(id);
        delivery.setDeleteTime(new Date());
        int update = this.deliveryDao.update(delivery);
        return update == 1;
    }

    @Override
    public List<Delivery> getAll(Long universityId) {
        List<Delivery> all = deliveryDao.getAll(universityId);
        return all;
    }

    @Override
    public Paging<Delivery> page(Integer page, Integer size) {
        List<Delivery> list = this.queryAllByLimit(page, size);
        long totalPage = deliveryDao.getTotalPage();
        Long total = size >= totalPage ? totalPage : size;
        return new Paging<>(total, totalPage, page, size, list);
    }

    @Override
    public Paging<Delivery> pageByUser(String authorization, Integer page, Integer size) {
        Map<String, String> claimMap = jwtService.getClaimMap(authorization);
        long uid = Long.parseLong(claimMap.get("uid"));
        User user = userDao.queryById(uid);
        List<Delivery> list = deliveryDao.queryAllByLimitByUniversityId(user.getUniversityId(),page, size);
        long totalPage = deliveryDao.getTotalPage();
        Long total = size >= totalPage ? totalPage : size;
        return new Paging<>(total, totalPage, page, size, list);
    }


}